/**
 * Created by DEV005 on 2017/8/23.
 */
import React, {Component} from 'react';
import {View, ListView, ActivityIndicator, BackHandler} from 'react-native';

import {Navigator} from 'react-native-deprecated-custom-components';
import MySuggestList from './pages/MySuggestList';
import CoordinationList from './pages/CoordinationList';
import AppService from "./service/AppService";
import {width, height} from "./utils/getSize";
import { getHeaderPadding } from "./utils/utils";
import RConfig from './language/RConfig';
import {getPassportId} from './service/rpc';

global.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

RConfig.language = 'zh';

class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            homePage: "MySuggestList",
            isLoading: true,
        }
    }

    componentWillMount() {
        let that = this;
        global.isCoordinator = false;
        // NativeModules.security.getUnionId().then((result) => {
        //         global.unionId = result;
        //     }
        // ).catch((error) => {
        //     console.log(error)
        // });
        getPassportId().then((result) => {
                global.passportId = result;
                AppService.getPassport({id: result}).then(data => {
                    global.tenantId = data.passport.tenantId;
                    global.userId = data.passport.userId;
                    global.userName = data.passport.userName;
                    global.functionList = data.passport.functionList;
                    AppService.findSysProduct({code: "com.xiniunet.xntalk"}).then(data => {
                        if (data.result && data.result[0] && data.result[0].id) {
                            let productId = data.result[0].id;
                            AppService.getAuthList({productId: productId}).then(data => {
                                if (data.result && data.result.length > 0) {
                                    for (let i = 0; i < data.result.length; i++) {
                                        if ("suggest.mobile.coordinator" == data.result[i]) {
                                            global.isCoordinator = true;
                                            that.state.homePage = "CoordinationList";
                                            break;
                                        }
                                    }
                                    global.functionList = data.result;
                                    that.setState({isLoading: false});
                                } else {
                                    that.setState({isLoading: false});
                                }
                            });
                        } else {
                            that.setState({isLoading: false});
                        }
                    });
                });
            }
        ).catch((error) => {
            console.log(error)
        });
    }

    componentDidMount() {
        BackHandler.addEventListener('hardwareBackPress', this.onAndroidBackPress);
    }

    componentWillUnmount() {
        BackHandler.removeEventListener('hardwareBackPress', this.onAndroidBackPress);
    }

    onAndroidBackPress = () => {
        const nav = this.navigatorRef;
        const routers = nav.getCurrentRoutes();
        if (routers.length > 1) {
           nav.pop();
           return true;
        }
        // 参考群里链接 https://www.cnblogs.com/shaoting/p/6340998.html
        // return false 是退出app, 一般是连续点击两次, 这里没要求
        // 这里使用 调用原生提供的返回方法
        // NativeModules.system.navTo("BACK");
        return true;
    };


    getHomePage = () => {
        if (this.state.homePage === "MySuggestList") {
            return {name: 'MySuggestList', component: MySuggestList}
        }
        return {name: 'CoordinationList', component: CoordinationList}
    };


    render() {
        const {isLoading} = this.state;
        return (
            <View style={{flex: 1, paddingTop: getHeaderPadding(), backgroundColor: '#333238'}}>
                {
                    isLoading ? (
                        <ActivityIndicator
                            style={{width: width, height: height}}
                            size='large'
                            color='#39f'
                            animating={isLoading}/>
                    ) : (
                        <Navigator
                            initialRoute={this.getHomePage()}
                            ref={ node => this.navigatorRef = node}
                            configureScene={
                                (route) => {
                                    return Navigator.SceneConfigs.FloatFromRight;
                                }
                            }
                            renderScene={
                                (route, navigator) => {
                                    const Component = route.component;
                                    return <Component{...route.params} navigator={navigator}/>;
                                }
                            }
                        />
                    )
                }
            </View>
        );
    }
}

export default App;
